package com.example.myapplication.part03.part12;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.example.myapplication.R;

import java.util.List;

public class FlowerAdapter extends RecyclerView.Adapter<FlowerAdapter.ViewHolder> {
    private List<Flower> mFlowerList;

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView flowerImage;
        TextView flowerName;

        public ViewHolder(View view) {
            super(view);
            flowerImage = view.findViewById(R.id.fruit_image);
            flowerName = view.findViewById(R.id.fruit_name);
        }
    }

    public FlowerAdapter(List<Flower> flowerList) {
        mFlowerList = flowerList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.flower_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Flower flower = mFlowerList.get(position);
        holder.flowerImage.setImageResource(flower.getImageId());
        holder.flowerName.setText(flower.getName());
    }

    @Override
    public int getItemCount() {
        return mFlowerList.size();
    }
}